package com.codefish.codefish_blog.controller;

import com.codefish.codefish_blog.ajax.AjaxResult;
import com.codefish.codefish_blog.config.property.BlogWebProperties;
import com.codefish.codefish_blog.entity.User;
import com.codefish.codefish_blog.service.ArticleLikeRecordService;
import com.codefish.codefish_blog.service.CommentLikeRecordService;
import com.codefish.codefish_blog.service.CommentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * 文章数据相关controller类
 *
 * @author codefish
 * @version 1.0
 * @date 2022/03/27 下午 07:18
 */
@RestController
@RequestMapping("/articleOpe")
public class ArticleDataController {
    @Autowired
    ArticleLikeRecordService articleLikeRecordService;
    @Autowired
    CommentRecordService commentRecordService;
    @Autowired
    CommentLikeRecordService commentLikeRecordService;

    /**
     * 用户对文章的点赞操作
     *
     * @param articleId 点赞的文章id
     * @param request   request对象
     * @return 点赞操作结果
     */
    @PostMapping("/like")
    public AjaxResult likeArticle(long articleId, HttpServletRequest request) {
        User userInfo = (User) request.getAttribute(BlogWebProperties.USER_INFO_PROPERTY);
        if (userInfo == null) {
            return AjaxResult.fail("点赞文章失败");
        }
        articleLikeRecordService.addLikeRecord(articleId, userInfo.getId());
        return AjaxResult.success("点赞文章成功");
    }

    /**
     * 用户对文章的取消点赞操作
     *
     * @param articleId 取消点赞的文章id
     * @param request   request对象
     * @return 取消点赞操作结果
     */
    @PostMapping("/unlike")
    public AjaxResult unlikeArticle(long articleId, HttpServletRequest request) {
        User userInfo = (User) request.getAttribute(BlogWebProperties.USER_INFO_PROPERTY);
        if (userInfo == null) {
            return AjaxResult.fail("取消点赞文章失败");
        }
        articleLikeRecordService.removeLikeRecord(articleId, userInfo.getId());
        return AjaxResult.success("取消点赞文章成功");
    }

    /**
     * 用户对评论点赞
     *
     * @param commentId 点赞评论id
     * @param request   request对象
     * @return 评论点赞结果
     */
    @PostMapping("/likeComment")
    public AjaxResult likeComment(Long commentId, HttpServletRequest request) {
        User userInfo = (User) request.getAttribute(BlogWebProperties.USER_INFO_PROPERTY);
        if (userInfo == null) {
            return AjaxResult.fail("点赞评论失败");
        }
        commentLikeRecordService.addLikeRecord(commentId, userInfo.getId());
        return AjaxResult.success("点赞评论成功");
    }

    /**
     * 用户对评论取消点赞
     *
     * @param commentId 取消点赞评论id
     * @param request   request对象
     * @return 取消评论点赞结果
     */
    @PostMapping("/unlikeComment")
    public AjaxResult unlikeComment(Long commentId, HttpServletRequest request) {
        User userInfo = (User) request.getAttribute(BlogWebProperties.USER_INFO_PROPERTY);
        if (userInfo == null) {
            return AjaxResult.fail("取消点赞评论失败");
        }
        commentLikeRecordService.removeLikeRecord(commentId, userInfo.getId());
        return AjaxResult.success("取消点赞评论成功");
    }


    /**
     * 用户评论文章操作(一级评论)
     *
     * @param content   评论内容
     * @param articleId 评论的文章id
     * @param request   request对象
     * @return 评论操作结果
     */
    @PostMapping("/comment")
    public AjaxResult publishComment(String content, long articleId, HttpServletRequest request) {
        User user = (User) request.getAttribute(BlogWebProperties.USER_INFO_PROPERTY);
        if (user == null) {
            return AjaxResult.fail("一级评论失败");
        }
        commentRecordService.addCommentOnArticle(articleId, content, user.getId());
        return AjaxResult.success("一级评论成功");
    }

    /**
     * 用户回复评论(二级评论)
     *
     * @param content   回复内容
     * @param articleId 文章id
     * @param commentId 被回复评论id
     * @param request   request对象
     * @return 二级评论操作结果
     */
    @PostMapping("/secondComment")
    public AjaxResult publishSecondComment(String content, long articleId, long commentId, HttpServletRequest request) {
        User user = (User) request.getAttribute(BlogWebProperties.USER_INFO_PROPERTY);
        if (user == null) {
            return AjaxResult.fail("二级评论失败");
        }
        commentRecordService.replyComment(articleId, commentId, content, user.getId());
        return AjaxResult.success("二级评论成功");
    }
}
